package com.kingsalert.kingsalert.activity.ln;

import static com.kingsalert.kingsalert.config.UserState.*;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.base.BaseActivity;
import com.kingsalert.kingsalert.base.BaseLnFragment;
import com.kingsalert.kingsalert.bean.process.ProcessInfo;
import com.kingsalert.kingsalert.config.UserState;
import com.kingsalert.kingsalert.fragment.ln_state.LnRpyFragment;
import com.kingsalert.kingsalert.fragment.ln_state.LnStateFailFragment;
import com.kingsalert.kingsalert.fragment.ln_state.LnStateWaitFragment;
import com.kingsalert.kingsalert.fragment.main.AgreementFragment;
import com.kingsalert.kingsalert.fragment.main.HomeFragment;
import com.kingsalert.kingsalert.fragment.main.PersonalCenterFragment;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends BaseActivity {

    private View homeView, pcView,dsView;
    private ImageView homeIv, pcIv,dsIv;
    private TextView homeTv, pcTv,dsTv;

    private BaseLnFragment baseLnFragment;
    private PersonalCenterFragment pcFragment;
    private AgreementFragment dsFragment;

    private int pageFlag = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        page = "MAIN";

        homeView = findViewById(R.id.am_view_home);
        pcView = findViewById(R.id.am_view_me);
        dsView = findViewById(R.id.am_view_ds);
        homeIv = findViewById(R.id.am_iv_home);
        pcIv = findViewById(R.id.am_iv_me);
        dsIv = findViewById(R.id.am_iv_ds);
        homeTv = findViewById(R.id.am_tv_home);
        pcTv = findViewById(R.id.am_tv_me);
        dsTv = findViewById(R.id.am_tv_ds);

        init();
    }

    private void init(){

        pcFragment = new PersonalCenterFragment();
        dsFragment = new AgreementFragment();

        homeView.setOnClickListener(v -> toHome());
        pcView.setOnClickListener(v -> toPc());
        dsView.setOnClickListener(v -> toDs());

        //初始化用户状态
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.am_layout_fragment,pcFragment).add(R.id.am_layout_fragment,dsFragment).commitAllowingStateLoss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUserState();
    }

    /**
     * 判断用户流程状态，如果状态有改变再更新页面
     */
    private void updateUserState(){
        //判断用户流程状态
        String state = ProcessInfo.getInstance().getUserState();
        if (UserState.isStateChange(state)) updatePageState(state);
    }

    private void updatePageState(String userState) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (baseLnFragment != null) fragmentTransaction.remove(baseLnFragment);

        switch (userState){
            case State_Rp:
                baseLnFragment = new LnRpyFragment();
                break;
            case State_Wait:
                baseLnFragment = new LnStateWaitFragment();
                break;
            case State_Fail:
                baseLnFragment = new LnStateFailFragment();
                break;
            default:
                baseLnFragment = new HomeFragment();
        }

        fragmentTransaction.add(R.id.am_layout_fragment, baseLnFragment);

        switch (pageFlag) {
            case 2:
                fragmentTransaction.show(pcFragment).hide(baseLnFragment).hide(dsFragment);
                break;
            case 3:
                fragmentTransaction.show(dsFragment).hide(baseLnFragment).hide(pcFragment);
                break;
            default:
                fragmentTransaction.show(baseLnFragment).hide(pcFragment).hide(dsFragment);
        }

        fragmentTransaction.commitAllowingStateLoss();
    }


    private void toHome(){

        HttpUtil.getInstance().saveBuriedPoint(page,"HOME_CLICK","");

        pageFlag = 1;

        homeIv.setBackgroundResource(R.drawable.icon_home_true);
        pcIv.setBackgroundResource(R.drawable.icon_pc_false);
        dsIv.setBackgroundResource(R.drawable.icon_ds_false);

        homeTv.setTextColor(getColor(R.color.tab_text_true));
        pcTv.setTextColor(getColor(R.color.tab_text_false));
        dsTv.setTextColor(getColor(R.color.tab_text_false));

        getSupportFragmentManager().beginTransaction().show(baseLnFragment).hide(pcFragment).hide(dsFragment).commitAllowingStateLoss();

    }

    private void toPc(){

        HttpUtil.getInstance().saveBuriedPoint(page,"ME_CLICK","");

        pageFlag = 2;

        homeIv.setBackgroundResource(R.drawable.icon_home_false);
        pcIv.setBackgroundResource(R.drawable.icon_pc_true);
        dsIv.setBackgroundResource(R.drawable.icon_ds_false);

        homeTv.setTextColor(getColor(R.color.tab_text_false));
        pcTv.setTextColor(getColor(R.color.tab_text_true));
        dsTv.setTextColor(getColor(R.color.tab_text_false));

        getSupportFragmentManager().beginTransaction().show(pcFragment).hide(baseLnFragment).hide(dsFragment).commitAllowingStateLoss();

    }

    private void toDs(){

        HttpUtil.getInstance().saveBuriedPoint(page,"STATEMENT_CLICK","");

        pageFlag = 3;

        homeIv.setBackgroundResource(R.drawable.icon_home_false);
        pcIv.setBackgroundResource(R.drawable.icon_pc_false);
        dsIv.setBackgroundResource(R.drawable.icon_ds_true);

        homeTv.setTextColor(getColor(R.color.tab_text_false));
        pcTv.setTextColor(getColor(R.color.tab_text_false));
        dsTv.setTextColor(getColor(R.color.tab_text_true));

        getSupportFragmentManager().beginTransaction().show(dsFragment).hide(pcFragment).hide(baseLnFragment).commitAllowingStateLoss();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UserState.clearState();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (baseLnFragment!=null && baseLnFragment instanceof HomeFragment) EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,baseLnFragment);
    }
}